package yl.hs.bmipfull.service.system.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yl.hs.bmipfull.daos.system.ICoreDataTagDao;
import yl.hs.bmipfull.daos.system.IDutyInfoDao;
import yl.hs.bmipfull.pojo.system.DutyInfoEntity;
import yl.hs.bmipfull.service.system.IDutyInfoService;
import yl.hs.bmipfull.service.system.IOrganizationInfoService;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作用:  职位信息服务层接口实现<br/>
 * 创建时间: 2021年05月12日 15时51分48秒 <br/>
 *
 * @author 张凯
 */
@Service
public class DutyInfoServiceImpl extends SystemBaseService implements IDutyInfoService {


    @Autowired
    IDutyInfoDao dao;
    @Autowired
    IOrganizationInfoService orgSvc;
    @Autowired
    ICoreDataTagDao tagDao;

    @Override
    public List<DutyInfoEntity> queryAll() {
        return dao.queryAll();
    }

    @Override
    public PageResult<DutyInfoEntity> queryList(PageQuery pageQuery, int rowNum, int page) throws Exception {
        var result = new PageResult<DutyInfoEntity>(rowNum);
        var query = pageQuery.getQuery();
        pageQuery.checkSortSides(DutyInfoEntity.class);
        result.setRecords(dao.queryPageCount(query));
        result.setPage(page);
        query.put("querySkip", (page - 1) * rowNum);
        query.put("pageSize", rowNum);
        query.put("sortSide", String.join(",", pageQuery.getSortSides()));
        result.setData(dao.queryList(query));
        return result;
    }

    @Override
    public DutyInfoEntity findBySn(String sn) {
        return dao.findBySN(sn);
    }

    @Transactional
    public Integer saveDutyInfo(Map<String, Object> model, String sn) throws Exception {
        Integer result = 0;
        if (Tl.isEmpty(sn)) {
            //执行保存
            DutyInfoEntity entity = new DutyInfoEntity();
            Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(DutyInfoEntity.class));
            if (entity.getState() == null) {
                entity.setState((short) 1);
            }
            if (!Tl.isEmpty(entity.getUnitSN())) {
                entity.setOrganizationCode(orgSvc.findUnitCodeBySN(new String[]{entity.getUnitSN()}));
            }

            result = dao.insertNew(entity);
            if (result > 0) {
                writeLog(ActionType.CREATE, entity.getSn(), "插入职位信息信息", Tl.toJson(entity), DutyInfoEntity.class);
            }

        } else {
            //执行修改
            DutyInfoEntity entity = findBySn(sn);
            var backJson = entity.toJson();
            var changeLog = Tl.getChangeLog(entity, model);
            entity.setPYFieldValue();

            result = dao.update(entity);
            if (result > 0) {
                tagDao.updateTag(entity.getSn(), entity.getDutyName());
                writeLog(ActionType.UPDATE, sn, changeLog, backJson, DutyInfoEntity.class);
            }
        }

        return result;
    }

    @Transactional
    public Integer delete(String[] sn) {
        Integer result = dao.changeStateBySN(sn, DataState.DELETED.code);
        if (result > 0) {
            tagDao.deleteTagByTagSN(sn);
            writeLog(ActionType.DELETE, sn, DutyInfoEntity.class);
        }
        return result;
    }

    @Override
    public List<DutyInfoEntity> queryListByUnitSN(String[] unitSN, String key) {
        return dao.queryListByUnitSN(unitSN, key);
    }

    @Override
    public Map<String, Object> getAllDutyMap() {
        var obj = new HashMap<String,Object>();
        var allDuty = dao.queryAll();
        if(allDuty.size()>0) {
            for (var i = 0; i < allDuty.size(); i++) {
                var duty = allDuty.get(i);
                if (!obj.containsKey(duty.getSn())) {
                    obj.put(duty.getSn(), duty.getDutyName());
                }
            }
        }
        return obj;
    }
}